package com.wzh.sending;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.wzh.sending.adapter.FindAdapter;
import com.wzh.sending.callback.LocationInterface;
import com.wzh.sending.info.UsersInfo;
import com.wzh.sending.utils.LocationUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.update.BmobUpdateAgent;

/**
     *  所有用户列表
     */
public class FindUserActivity extends CheckPermissionsActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.my_list)
    ListView myList;
    @Bind(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    FindAdapter findAdapter;
    List<UsersInfo> usersInfoList;
    LocationUtil locationUtil;
    double lot, lat;
    @Bind(R.id.my_switch)
    CheckBox mySwitch;
    SharedPreferences preferences;
    @Bind(R.id.progress)
    ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_user);
        ButterKnife.bind(this);
        BmobUpdateAgent.update(this);
        initView();
    }

    private void initView() {
        myList.setEmptyView(progress);
        preferences = getBaseContext().getSharedPreferences("loc", Context.MODE_PRIVATE);
        setBarTitle(toolbar, "全部信息");
        if (preferences.contains("issend")) {
            mySwitch.setChecked(preferences.getBoolean("issend", false));
        }
        usersInfoList = new ArrayList<>();
        findAdapter = new FindAdapter(getBaseContext(), usersInfoList,imageLoader);
        myList.setAdapter(findAdapter);

        locationUtil = new LocationUtil(this);
        locationUtil.initLocation();
        locationUtil.startLocation();
        locationUtil.setLocationInterface(new LocationInterface() {
            @Override
            public void getLocation(AMapLocation loc) {
                if (loc != null) {
                    lat = loc.getLatitude();
                    lot = loc.getLongitude();
                    findAdapter.setLotandLat(lot, lat);
                    locationUtil.stopLocation();
                    locationUtil.destroyLocation();
                    addData();
                } else {
                    mySrf.setRefreshing(false);
                }
            }
        });
        initEvent();
    }

    private void initEvent() {
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                preferences.edit().putBoolean("issend", b).commit();
                if (b) {
                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
        mySrf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                usersInfoList.clear();
                findAdapter.notifyDataSetChanged();
                locationUtil.initLocation();
                locationUtil.startLocation();
            }
        });

        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(getBaseContext(), UserActivity.class);
                intent.putExtra("user", usersInfoList.get(i));
                startActivity(intent);
            }
        });
    }

    private void addData() {
        BmobQuery<UsersInfo> query = new BmobQuery<UsersInfo>();
        query.setLimit(500);
        query.order("updateAt");
        query.findObjects(new FindListener<UsersInfo>() {
            @Override
            public void done(List<UsersInfo> list, BmobException e) {
                Log.e("LIST====", list.size() + "");
                if (e == null) {
                    if (list.size() != 0) {
                        usersInfoList.addAll(list);
                        Collections.reverse(usersInfoList);
                    }
                    findAdapter.notifyDataSetChanged();
                    mySrf.setRefreshing(false);
                } else {
                    Toast.makeText(getBaseContext(), "查询失败", Toast.LENGTH_SHORT).show();
                    mySrf.setRefreshing(false);
                }
            }
        });
    }

}
